package com.niit.project.service.impl;

import com.niit.project.dao.UserDao;
import com.niit.project.dao.impl.UserDaoImpl;
import com.niit.project.entity.User;
import com.niit.project.service.UserService;
import com.niit.project.utils.Dbutils;


public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public User login(String username, String password) {
        User user = null;
        try {
            //这里表示事务的控制，开启事务
            Dbutils.begin();
            //选取所有用户名
            User temp = userDao.select(username);
            //如果用户名存在则进行密码的判断
            if(temp!=null)
            {
                System.out.println("用户名存在！");
                if(temp.getPassword().equals(password))
                {
                    user = temp;
                }
            }
            //事务的提交
            Dbutils.commit();
        } catch (Exception e) {
            //事务的回滚
            Dbutils.rollback();
            throw new RuntimeException(e);
        }
        return user;
    }

    //用户的注册
    @Override
    public int register(User user) {
        int result = 0;
        try {
            Dbutils.begin();
            //将用户信息插入数据库中
            result = userDao.insert(user);
            Dbutils.commit();
        } catch (Exception e) {
            Dbutils.rollback();
            throw new RuntimeException(e);
        }

        return result;

    }
}
